package com.dries.security.core.validate.code.captcha.sms;

import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2020/9/7 16:41
 */
@Slf4j
public class DefaultSmsCodeSender implements SmsCodeSender {

    /**
     * 发送验证码
     * @param mobile
     * @param code
     */
    @Override
    public void send(String mobile, String code) {

        log.info("向手机【" + mobile + "】发送短信验证码: " + code);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIeXAMLldj63MX", "hShC2dsTrRkiU5tdyP8MySY46kNWM9");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("SignName", "遐享科技");
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("TemplateCode", "SMS_162441330");
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        request.putQueryParameter("TemplateParam", JSONUtil.toJsonStr(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            // System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
}
